package com.lelouch.examplecode.io;

import java.io.*;

/**
 * 字节流和字符流
 *
 * @author lcy
 * @create 2018-12-03 15:37
 */
public class StreamAnd {

    public static void main(String[] args) {
        //readTxt();
        writeTxt();
    }

    public static void  readTxt(){
        File file = new File("E:\\IOTest\\dir1\\test.txt");
        FileReader fileReader =null;
        try {
            fileReader = new FileReader(file);
            char[] buf = new char[1024];
            int length=0;
            int count =0;
            while((length=fileReader.read(buf))!=-1){
                System.out.println(buf);
                System.out.println(++count);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileReader!=null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    public static void  writeTxt(){
        File file = new File("E:\\IOTest\\dir1\\test.txt");
        FileWriter fileWriter =null;
        try {
            fileWriter = new FileWriter(file);
            fileWriter.write("你好");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileWriter!=null){
                try {
                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
